local Icon = surface.GetTextureID( "wpp/wpp" )
local Gradient = surface.GetTextureID( "vgui/gradient_down" )


local function DrawAccessBox(X,Y,Name,Bool,Align)
	
	local C = Color(100,0,0,255)
	if ( Bool ) then C = Color(0,100,0,255) end
	
	surface.SetFont("ChatFont")
	local FW,FH = surface.GetTextSize(Name)
	local W = (FW + 4) / 2
	if ( W < 60 ) then W = 60 end
	
	if ( Align ) and ( Align == TEXT_ALIGN_RIGHT ) then
		X = X - W
	elseif ( Align ) and ( Align == TEXT_ALIGN_LEFT ) then
		X = X + W
	end
	
	surface.SetDrawColor(0,0,0,225)
	surface.SetTexture(Gradient)
	surface.DrawTexturedRectRotated(X - (W/2),Y,20,W,-90)
	surface.DrawTexturedRectRotated(X + (W/2),Y,20,W,90)
	
	surface.SetDrawColor(C)
	
	surface.DrawLine(X - W,Y + 10,X + W,Y + 10)
	surface.DrawLine(X - W,Y - 10,X + W,Y - 10)
	
	surface.SetTexture(Icon)
	surface.DrawTexturedRect(X - 20,Y - 20,40,40)
	
	draw.SimpleText(Name,"ChatFont",X,Y,C,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER)
	
end


local function DrawOnCenter(Entity,Key)
	if ( !Entity.WPP ) then return end
	local Pos = Entity:LocalToWorld( Entity:OBBCenter() ):ToScreen()
	local Bool = LocalPlayer():WPP_EntityCheck(Entity,Key)
	DrawAccessBox(Pos.x,Pos.y,Entity.WPP.Name,Bool)
end

local HudVar = CreateClientConVar("wpp_hud_mode","0",true,false)

hook.Add("HUDPaint","WPP",function()
	local Player = LocalPlayer()
	local Key = Player:WPP_WeaponAsKey()
	if ( !Key ) then return end
	
	local Entity = Player:GetEyeTrace().Entity
	if ( Entity ) and ( HudVar:GetInt() == 0 ) then
		if ( !Entity.WPP ) then return end
		local Bool = LocalPlayer():WPP_EntityCheck(Entity,Key)
		DrawAccessBox(ScrW() - 20,ScrH() / 2,Entity.WPP.Name || "Disconected",Bool,TEXT_ALIGN_RIGHT)
		
	elseif ( Entity ) and ( HudVar:GetInt() == 1 ) then
		DrawOnCenter(Entity,Key)
		
	elseif ( HudVar:GetInt() >= 2 ) then
		local List = ents.FindInSphere(Player:GetPos(),500)
		if ( #List == 0 ) then return end
		for _,Entity in pairs ( List ) do
			DrawOnCenter(Entity,Key)
		end
	end
end)